<template>
	<view>
		<!-- 添加赠品 -->
		<header-top :messageInfo="{typeFlag:true,message:'添加赠品',liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content">
					<view class="nostrongtext" @click="surebtn">确定</view>
				</view>
			</view>
		</header-top>
		<view class="mainpadding ffffff margin_top" v-for="(item,index) in list" :key="index">
			<view class="titletext">{{item.goods_name}}</view>
			<view class="flexbuju margin_top" @click="shengchang(item,index)">
				<view class="huisetext">生产日期：</view>
				<input type="text" class="inputs xiabiank" placeholder="无生产日期" v-model='item.manufacture' disabled="">
				<view class="sanjiaox" ></view>
			</view>
			<view class="flexbuju margin_top">
				<view class="huisetext">数量：</view>
				<input type="text" class="inputs xiabiank" placeholder="输入数量" v-model="item.num">
				<view class="">{{item.unit_name}}</view>
			</view>
			<view class="flexbuju margin_top">
				<view class="huisetext">备注：</view>
				<input type="text" class="inputs xiabiank" placeholder="请输入备注" v-model="item.note">
			</view>
			<view class="flexbetween">
				<view class="nostrongtext margin_top">库存：{{item.stock || 0}}
					<text v-if="!item.is_kc" style="color: #ff0000;margin-left: 5rpx;"> 库存不足</text>
				</view>
				<view :class="item.num<=Number(item.number) - Number(item.used)?'huisetext1':'hongsetext'">{{item.num || 0}}/{{ Number(item.number) - Number(item.used)}}使用次数</view>
			</view>
			
		</view>
		<u-datetime-picker :show="timeshow" mode="date" v-model="value1" @confirm="timersure" @cancel="timeshow=false">
		</u-datetime-picker>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				getmsg: {
					ck_id: '6', //仓库id
					sp_code: '', //商品编号
					mr_sjstate: 'wholesale_price', //默认价格
					goods_ids: [1,2], //id数组群
				},
				timeshow: false,
				value1: Number(new Date()),
				timeshow:false,
				list:[
					{
						manufacture:"",
						unit_name:"",
						number:"",
						note:"",
						goods_name:"",
						id:"",
						stock:"",
						is_kc:true,
						goods_code:"",
						unit_id:"",
						used:"",
					}
				],
				selectindex:""
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.getmsg));
			this.getmsg = data
			this.init()
		},
		watch:{
			list:{
				handler(newval){
					newval.forEach(item=>{
						if(Number(item.num)>Number(item.stock)){
							item.is_kc = false
						}else{
							item.is_kc = true
						}
					})
				},
				deep:true
			}
		},
		methods: {
			init() {
				let _this = this;
				httpRequest.request('appapi/manage/goods/gifts_detail', 'POST', {
					gifts_ids: this.getmsg.goods_ids,
					store_id: this.getmsg.ck_id
				}, false, false, true).then(res => {
					this.list = res.data
					this.list.forEach(item=>{
						this.$set(item,'manufacture','')
						this.$set(item,'is_kc',true)
						this.$set(item,'num',"")
						item.note=""
					})
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			// 提交
			surebtn(){
				let arr = []
				let flag = true
				this.list.forEach(item=>{
					let num1 = Number(item.number) - Number(item.used)
					if(item.num>num1){
						flag = false
					}
					arr.push({
						gifts_id:item.id,
						goods_code:item.goods_code,
						unit:item.unit_id,
						number:item.num,						
						note:item.note,
						manufacture:item.manufacture
					})
				})
				if(flag==false){
					httpRequest.toast('您赠送的商品中出现了超额赠送情况，请检查后重试')
					return
				}
				httpRequest.request('appapi/manage/goods/sales_add_gift', 'POST', {
					purchase_code: this.getmsg.sp_code,
					goods_info:arr
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(function(){
							uni.navigateBack({
								delta: 3
							})
						},1000)
						
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			//生产日期
			shengchang(item, index) {
				if (item.batch == 0) {
					return false
				}
				this.selectindex = index
				this.timeshow = true
			},
			timersure(e) {
				this.list[this.selectindex].manufacture = this.date(e.value)
				this.timeshow = false
			},
			date(timer) {
				var date = new Date(timer); //当前时间
				var year = date.getFullYear() //返回指定日期的年份
				var month = repair(date.getMonth() + 1); //月
				var day = repair(date.getDate()); //日
				var curTime = year + "-" + month + "-" + day
			
				function repair(i) {
					if (i >= 0 && i <= 9) {
						return "0" + i;
					} else {
						return i;
					}
				}
				return curTime
			},
		}
	}
</script>

<style>
	.nostrongtext{
		padding-bottom: 20rpx;
	}
	.hongsetext{
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #ff0000;
	}
	.allbtn {
		border-radius: 43rpx;
		padding: 10rpx 50rpx;
		border: 2rpx solid #E7E7E7;
	}
	
	.lansebtn {
		border-radius: 0 43rpx 43rpx 0;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0BBDCB;
	}
	
	.shanchu {
		border-radius: 43rpx 0 0 43rpx;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
		border-right: none;
	}
	
	.smalltext {
		font-size: 22rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}
	
	.sanjiaox {
		border: 14rpx solid transparent;
		border-top: 14rpx solid #666666;
	}
	
	.inputsl {
		width: 25%;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		text-align: center;
	}
	
	.flexbuju {
		display: flex;
		align-items: flex-start;
	}
	
	.inputs {
		width: 40%;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		text-align: center;
	}
	
	.huisetext {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
		
		min-width: 150rpx;
	}
	.huisetext1 {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}
	
	.icon {
		background-image: url('@/static/image/system/xiao.png');
		width: 108rpx;
		height: 52rpx;
		margin-right: 34rpx;
		background-size: 100% 100%;
	}
	
	.icontow {
		background-image: url('@/static/image/system/tui.png');
		width: 108rpx;
		height: 52rpx;
		margin-right: 34rpx;
		background-size: 100% 100%;
	}
	
	.jiahao {
		width: 43.33rpx;
		height: 43.33rpx;
		background-image: url('@/static/image/system/jiahao.png');
		background-size: 100% 100%;
	}
	
	.xiabiank {
		padding-bottom: 20rpx;
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.03);
	}
</style>
